package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.StateBean;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * 我的消息
 */

public class ALLMessageActivity extends BaseActivity {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_me_message_count)
    TextView tvMeMessageCount;
    @BindView(R.id.tv_system_message_count)
    TextView tvSystemMessageCount;

    @Override
    protected int setLayout() {
        return R.layout.activity_all_message;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("我的消息");
        RxBus.get().register(this);
        getData();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData();

    }

    private void getData() {
        ApiCreator.getApi().notReadCount()
                .compose(RxTransformer.<StateBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<StateBean>() {
                    @Override
                    protected void onSuccess(StateBean data) {
                        if (data.getPlatformNotReadNum() > 0) {
                            tvMeMessageCount.setVisibility(View.VISIBLE);
                            tvMeMessageCount.setText(data.getPlatformNotReadNum()+"");
                        } else {
                            tvMeMessageCount.setVisibility(View.GONE);
                        }
                        if (data.getSystemNotReadNum() > 0) {
                            tvSystemMessageCount.setVisibility(View.VISIBLE);
                            tvSystemMessageCount.setText(data.getSystemNotReadNum()+"");
                        } else {
                            tvSystemMessageCount.setVisibility(View.GONE);
                        }
                    }
                });
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    @OnClick(R.id.ll_me_message)
    void onMeMessage() {
        startActivity(MeMessageActivity.class);
    }

    @OnClick(R.id.ll_system_message)
    void onSystemMessage() {
        startActivity(MessageActivity.class);
    }

    @Subscribe(code = RxBusCode.TO_ShareOrderFragment)
    public void toShareOrderFragment() {
        finish();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
